package edu.tjpu.share.task;

import java.util.HashMap;

import android.os.AsyncTask;
import android.os.Handler;
import edu.tjpu.share.app.Fields;
import edu.tjpu.share.newui.NewMessage;
import edu.tjpu.share.po.FileForUpload;
import edu.tjpu.share.util.FileUtil;

public class SaveFileListTask extends
		AsyncTask<HashMap<String, Object>, Integer, HashMap<String, Object>> {

	@Override
	protected HashMap<String, Object> doInBackground(
			HashMap<String, Object>... maps) {

		Handler handler = (Handler) maps[0].get(Fields.HANDLER);
		java.io.File cache = (java.io.File) maps[0].get(Fields.FILE_URL);
		FileForUpload upfile = (FileForUpload) maps[0]
				.get(Fields.FILE_FORUPLOAD);

		java.io.File cacheFile = new java.io.File(cache, Fields.FILE_LIST);

		FileUtil.writeObjectToFile(upfile, cacheFile.getAbsolutePath());

		HashMap<String, Object> outmap = new HashMap<String, Object>();
		outmap.put(Fields.HANDLER, handler);
		return outmap;
	}

	@Override
	protected void onPostExecute(HashMap<String, Object> result) {
		super.onPostExecute(result);
		if (isCancelled()) {
			result = null;
		}
		if (result != null) {
			Handler handler = (Handler) result.get(Fields.HANDLER);
			NewMessage.setStatus(true);
			handler.sendEmptyMessage(0);
		}
	}

}
